home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / windows / winthrea / threads.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  6.6 KB  |  260 lines

  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "thrdapi.h"
  5.  
  6. /********************************************************************
  7.  *                     Threads test application                     *
  8.  *                                                                  *
  9.  ********************************************************************
  10.  *       Copyright 1992 Robert Salesas, All Rights Reserved         *
  11.  ********************************************************************
  12.  *       Version: 1.00             Author:  Robert Salesas          *
  13.  *       Date:    30-Jan-1992      Changes: Original                *
  14.  *                                                                  *
  15.  ********************************************************************
  16.  *       Version: 1.50             Author:  Robert Salesas          *
  17.  *       Date:    22-May-1994      Changes: Original                *
  18.  *                                                                  *
  19.  ********************************************************************/
  20.  
  21. #define APPNAME (LPSTR)"BC++/TC++ Threads"
  22. #define APPFILE "THREADS.EXE"
  23. #define CLASSNAME "Threads"
  24.  
  25. HANDLE hInst;
  26.  
  27. FARPROC BallProc,
  28.         LineProc;
  29.  
  30.  
  31. /***** Thread functions *****/
  32.  
  33. COLORREF Colors[] = { 0x00FF0000,
  34.                       0x0000FF00,
  35.                       0x000000FF,
  36.                       0x00FFFF00,
  37.                       0x0000FFFF,
  38.                       0x00FF00FF,
  39.                       0x00C000C0 };
  40.  
  41.  
  42.  VOID FAR PASCAL _export LineThread(PThreadRec Thread,  HWND Wnd, WORD wParam, LONG lParam)
  43. {
  44.   HDC DC;
  45.   RECT Rect;
  46.   HPEN Pen,
  47.        OPen;
  48.   int X, Y, ColIndex;
  49.   COLORREF Col;
  50.  
  51.  
  52.  
  53.   GetClientRect(Wnd, &Rect);
  54.   X = random(Rect.right);
  55.   Y = random(Rect.bottom);
  56.  
  57.   Col = Colors[random(6)];
  58.   Pen = CreatePen(PS_SOLID, 1, Col);
  59.  
  60.   do {
  61.     DC = GetDC(Wnd);
  62.     if (DC == 0) {
  63.       DeleteObject(Pen);
  64.       ExitThread();
  65.     }
  66.  
  67.     OPen = SelectObject(DC, Pen);
  68.  
  69.     GetClientRect(Wnd, &Rect);
  70.     MoveTo(DC, X, Y);
  71.     X = max(0, min(Rect.right, X + random(91) - 45));
  72.     Y = max(0, min(Rect.bottom, Y + random(91) - 45));
  73.     LineTo(DC, X, Y);
  74.  
  75.     SelectObject(DC, OPen);
  76.     ReleaseDC(Wnd, DC);
  77.   } while (YieldThread() != TM_QUIT);
  78.  
  79.   DeleteObject(Pen);
  80.   ExitThread();
  81. }
  82.  
  83.  
  84. VOID FAR PASCAL _export BallThread(PThreadRec Thread, HWND Wnd, WORD wParam, LONG lParam)
  85. {
  86.   HDC DC;
  87.   RECT Rect;
  88.   int  XDir,
  89.        YDir,
  90.        X, OX,
  91.        Y, OY;
  92.   HICON Ball,
  93.      Erase;
  94.  
  95.   X = 0;
  96.   Y = 0;
  97.   XDir = 10 + (random(11) - 5);
  98.   YDir = 10 + (random(11) - 5);
  99.  
  100.   Ball = LoadIcon(hInst, MAKEINTRESOURCE(random(7) + 100));
  101.   Erase = LoadIcon(hInst, "ERASEBALL");
  102.  
  103.   do {
  104.     DC = GetDC(Wnd);
  105.     if (DC == 0)
  106.      ExitThread();
  107.  
  108.     GetClientRect(Wnd, &Rect);
  109.     OX = X;
  110.     OY = Y;
  111.     X = X + XDir;
  112.     Y = Y + YDir;
  113.  
  114.     if (X < 0) {
  115.       X = 0;
  116.       XDir = -(XDir - (random(11) - 5));
  117.       YDir = YDir + (random(11) - 5);
  118.     }
  119.     if (X + 32 > Rect.right) {
  120.       X = Rect.right - 32;
  121.       XDir = -(XDir - (random(11) - 5));
  122.       YDir = YDir + (random(11) - 5);
  123.     }
  124.  
  125.     if (Y < 0) {
  126.       Y = 0;
  127.       XDir = XDir - (random(11) - 5);
  128.       YDir = -(YDir + (random(11) - 5));
  129.     }
  130.     if (Y + 32 > Rect.bottom) {
  131.       Y = Rect.bottom - 32;
  132.       XDir = XDir + (random(11) - 5);
  133.       YDir = -(YDir + (random(11) - 5));
  134.     }
  135.  
  136.  
  137.     if ((XDir <= 0) && (XDir > -6))
  138.      XDir = -6;
  139.     if ((XDir > 0) && (XDir < 6))
  140.      XDir = 6;
  141.     if ((YDir <= 0) && (YDir > -6))
  142.      YDir = -6;
  143.     if ((YDir > 0) && (YDir < 6))
  144.      YDir = 5;
  145.     XDir = max(-20, min(20, XDir));
  146.     YDir = max(-20, min(20, YDir));
  147.  
  148.     DrawIcon(DC, OX, OY, Erase);
  149.     DrawIcon(DC, X, Y, Ball);
  150.     ReleaseDC(Wnd, DC);
  151.  
  152.   } while (YieldThread() != TM_QUIT);
  153.  
  154.   ExitThread();
  155. }
  156.  
  157.  
  158. /***** Window function *****/
  159.  
  160. LONG _export FAR PASCAL MainWndProc(HWND Window, WORD Msg, WORD wParam, DWORD lParam)
  161. {
  162.   char Title[256];
  163.   LONG NumThreads;
  164.  
  165.   switch (Msg) {
  166.     case WM_CREATE:
  167.       LineProc = MakeProcInstance((FARPROC)LineThread, hInst);
  168.       BallProc = MakeProcInstance((FARPROC)BallThread, hInst);
  169.       break;
  170.     case WM_COMMAND:
  171.       switch(wParam) {
  172.     case 100:
  173.       StartThread(BallProc, 2000, Window, 0, 0);
  174.       NumThreads = GetNumThreads();
  175.       wsprintf(Title, "%s - %d Threads", APPNAME, NumThreads);
  176.       SetWindowText(Window, Title);
  177.       break;
  178.     case 110:
  179.       SetThreadPriority(StartThread((FARPROC)LineProc, 2000, Window, 0, 0), TS_DEFPRIORITY / 2);
  180.       NumThreads = GetNumThreads();
  181.       wsprintf(Title, "%s - %d Threads", APPNAME, NumThreads);
  182.       SetWindowText(Window, Title);
  183.       break;
  184.  
  185.     case 500:
  186.       InvalidateRect(Window, NULL,TRUE);
  187.       break;
  188.     case 510:
  189.       EndTaskThreads(GetCurrentTask());
  190.       InvalidateRect(Window, NULL,TRUE);
  191.       NumThreads = GetNumThreads();
  192.       wsprintf(Title, "%s - %d Threads", APPNAME, NumThreads);
  193.       SetWindowText(Window, Title);
  194.       break;
  195.      }
  196.     break;
  197.  
  198.     case WM_DESTROY:
  199.       EndTaskThreads(GetCurrentTask());
  200.       FreeProcInstance(BallProc);
  201.       FreeProcInstance(LineProc);
  202.       PostQuitMessage(0);
  203.       break;
  204.  
  205.     default:
  206.       return DefWindowProc(Window, Msg, wParam, lParam);
  207.   }
  208. }
  209.  
  210.  
  211. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  212. {
  213.   HWND hWnd;
  214.   MSG msg;
  215.   WNDCLASS wndclass;
  216.   char Title[256];
  217.  
  218.   if(!hPrevInstance) {
  219.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  220.     wndclass.lpfnWndProc   = MainWndProc;
  221.     wndclass.cbClsExtra    = 0;
  222.     wndclass.cbWndExtra    = 0;
  223.     wndclass.hInstance     = 0;
  224.     wndclass.hIcon         = LoadIcon(0, IDI_APPLICATION);
  225.     wndclass.hCursor       = LoadCursor(0, IDC_ARROW);
  226.     wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  227.     wndclass.lpszMenuName  = (LPSTR)"APPMENU";
  228.     wndclass.lpszClassName = CLASSNAME;
  229.  
  230.     if(!RegisterClass(&wndclass)) {
  231.       MessageBox(NULL, "Unable to register window class.", NULL, MB_OK|MB_ICONSTOP);
  232.       return 0;
  233.     }
  234.   }
  235.  
  236.   hInst = hInstance;
  237.  
  238.   wsprintf(Title, "%s - 0 Threads", APPNAME);
  239.  
  240.   hWnd = CreateWindow(CLASSNAME,
  241.                       Title,
  242.                       WS_OVERLAPPEDWINDOW,
  243.                       CW_USEDEFAULT,
  244.                       0,
  245.                       CW_USEDEFAULT,
  246.                       0,
  247.                       0,
  248.                       0,
  249.                       hInstance,
  250.                       NULL);
  251.  
  252.   ShowWindow(hWnd, nCmdShow);
  253.  
  254.   while(GetMessage(&msg, NULL, 0, 0)) {
  255.     TranslateMessage(&msg);
  256.     DispatchMessage(&msg);
  257.   }
  258.   return msg.wParam;
  259. }
  260.